package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.graphics.BitmapFactory
import android.os.Bundle
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.INVITE_FRIEND_HTML
import cn.jishiyu11.xeqb.http.http.i.httpbase.INVITE_FRIEND_SHARE
import cn.jishiyu11.xeqb.http.http.i.httpbase.OkHttpManager
import cn.jishiyu11.xeqb.utils.ImageLoadUtils
import cn.jishiyu11.xeqb.utils.USER_INVICEDCODE
import cn.jishiyu11.xeqb.utils.showShare
import cn.jishiyu11.xeqb.utils.showShortToast
import cn.jishiyu11.xeqb.webview.X5WebView
import com.tencent.smtt.sdk.WebSettings
import kotlinx.android.synthetic.main.act_invite_friends.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException

/**
 * Created by jsy_zj on 2018-03-29.
 */
class InviteFriends : BaseActivity(), DataCallBack {


    private var shareBmUrl = ""
    private var shareTitle = ""
    private var shareInfo = ""
    private var shareLinks = ""
    private var title = "邀请好友"
    private var url = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_invite_friends)
        url = intent.getStringExtra("url")
        initView()
        getData()
    }

    override fun findViewById() {
    }


    override fun initView() {
        title_image.setOnClickListener { finish() }
        title_view.text = title

        act_invite_friends_web.loadUrl(INVITE_FRIEND_HTML)
        act_invite_friends_web!!.isHorizontalScrollBarEnabled = false
        act_invite_friends_web!!.isVerticalScrollBarEnabled = false
        //下面方法去掉
        val ix5 = act_invite_friends_web!!.x5WebViewExtension
        ix5?.setScrollBarFadingEnabled(false)



        act_invite_friends_button.setOnClickListener {
            //            shareLinks = "http://f4.market.xiaomi.com/download/AppStore/0237f14901e9a447725207e37ffa5f63b50888c09/cn.jishiyu11.xeqb.apk"
            this.showShare(shareTitle
                    , shareLinks
                    , shareInfo
                    , BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher)
                    , shareBmUrl
                    , shareLinks
            )
        }
    }

    fun getData() {
        var map = HashMap<String, String>()
        OkHttpManager.postAsync(INVITE_FRIEND_SHARE, "INVITE_FRIEND_SHARE", map, this)

    }

    override fun requestFailure(request: Request, name: String, e: IOException) {

    }

    override fun requestSuccess(result: String, name: String) {

        if (name.equals("INVITE_FRIEND_SHARE")) {

            println(result)
            var jsonObject = JSONObject(result)
            if (jsonObject.getString("code").equals("200")) {
                var jsonarray = JSONArray(jsonObject.getString("data"))
                jsonObject = jsonarray.get(0) as JSONObject

                shareBmUrl = jsonObject.getString("invitePic")
                shareTitle = jsonObject.getString("inviteTit")
                shareInfo = jsonObject.getString("inviteInfo")
                shareLinks = jsonObject.getString("inviteLink")

//                shareLinks = "https://www.jishiyu007.com/index.php?g=phone&m=share&a=bycode"

                if (shareLinks.contains("?")) {
                    shareLinks = shareLinks + "&${getString(R.string.inviteCode)}=$USER_INVICEDCODE&appID=110"
                } else {
                    shareLinks = shareLinks + "?${getString(R.string.inviteCode)}=$USER_INVICEDCODE&appID=110"
                }

            } else {
                showShortToast(jsonObject.getString("msg"))
            }
        }

    }

}
